package com.example.myspringai.volcengine.signer.example;


import com.example.myspringai.volcengine.signer.Credentials;
import com.example.myspringai.volcengine.signer.Signer;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.net.URI;

public class TranslationExample {

    public static void main(String[] args) throws Exception {
        System.out.println(translateToZh(translateToZh("nice!")));
    }

    public static String translateToZh(String content) throws Exception {
        if (content.length() >= 5000) {
            content = content.substring(0, 5000);
        }
        /* create credentials */
        Credentials credentials = new Credentials();
        credentials.setAccessKeyID("AKLTODY3YzBmYWVlMTA0NDVlMDg3YzIyOGFhZTQ2NDkzY2I");
        credentials.setSecretAccessKey("TlRrelpUQTRNR0l5TmpnMU5HTmpNV0kzTWpNM1kyUXpPVFpqT1RSa05UVQ==");
        credentials.setRegion("cn-north-1");
        credentials.setService("translate");

        /* create signer */
        Signer signer = new Signer();

        /* create http client */
        CloseableHttpClient httpClient = HttpClients.createDefault();

        /* prepare request */
        HttpPost request = new HttpPost();
        request.setURI(new URI("https://translate.volcengineapi.com?Action=TranslateText&Version=2020-06-01"));

        request.addHeader(HttpHeaders.USER_AGENT, "volc-sdk-java/v1.0.0");
        request.addHeader("Content-Type", "application/json");

        JsonObject payload = new JsonObject();
        payload.addProperty("TargetLanguage", "zh");

        // Create and add strings to a JsonArray
        JsonArray textArray = new JsonArray();
        textArray.add(content);

        payload.add("TextList", textArray);

        // Set payload to request entity
        StringEntity entity1 = new StringEntity(payload.toString(), HTTP.UTF_8);
        entity1.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        request.setEntity(entity1);


        // sign the request
        signer.sign(request, credentials);

        /* launch request */
        CloseableHttpResponse response = httpClient.execute(request);

        /* status code */
        System.out.println(response.getStatusLine().getStatusCode());   // 200

        /* get response body */
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity);
            // Parse the JSON response
            JsonElement jsonElement = JsonParser.parseString(result);
            JsonObject jsonObject = jsonElement.getAsJsonObject();

            // Get the TranslationList array
            JsonArray translationList = jsonObject.getAsJsonArray("TranslationList");

            // Get the first translation object
            JsonObject translationObject = translationList.get(0).getAsJsonObject();

            // Extract the Translation content
            return translationObject.get("Translation").getAsString();
        }
        /* close resources */
        response.close();
        httpClient.close();
        return "";
    }

}
